<?php





namespace app\admin\model;

use think\facade\Db;





class CustomerModel extends Common

{



    /**

     * @var string

     */

    protected $name = 'customer';



    protected $defaultSoftDelete = 0;



    protected $autoWriteTimestamp = true;



    /**

     * 驼峰格式转换

     * @var bool

     */

    protected $convertNameToCamel = true;



    protected $fail;



    /**

     * 获取订单公共条件

     * @param $shopIds

     * @param $request

     * @author ran

     * @date 2021-11-18 10:52

     * mailbox 466180170@qq.com

     */

    public function getCustomerCommonDataCollect($shopIds, $request)
    {

        $pc = $request['pc'] ?? 0;

        $isShop = $request['isShop'] ?? 0;

        $shop_person_id = $request['shop_person_id'] ?? 0;

        $sysModel = new SysModel();
        $position_id = $sysModel->getPersonPositionId($shop_person_id);
        $person_where  = [];
        if(!empty($position_id)){
            array_push($person_where,['nurse_id','=',$shop_person_id]);
        }

        if(empty($request['startTime'])){
            $startTime =strtotime(date('Y-m-01 00:00:00'));
        }else{
            $startTime =strtotime($request['startTime']);
        }

        if(empty($request['endTime'])){
            $endTime =strtotime(date('Y-m-d',strtotime(date('Y-m-1',strtotime('next month')).'-1 day')));
        }else{
            $endTime =strtotime($request['endTime']);
        }

        // 2023-09-14 调整结束时间戳
        $endTime = $endTime+86399;

        $jzxzTime=strtotime("2022-01-01");

        $shopIdmz = [73782775131388,73782774881835,73782775392774,73782775377728,73782775206631,73782775085647,73782774991693];


         //保有客户截止当前时间本店所有新单（首次消费金额500以上的 即之前给你对接说的 新单数据库 ）客户

        $tenure =Db::name('customer')

            ->where($person_where)

            ->when($shopIds ?? [], function ($query) use ($shopIds) {

                return $query->whereIn('shop_id', $shopIds);

            })->where('cj_time','>',0)

            ->where('cj_time','>',$jzxzTime)

            ->where('shop_id','not in',$shopIdmz)

            ->count();

        //流失预警

        $lc =Db::name('customer')

            ->where($person_where)

            ->when($shopIds ?? [], function ($query) use ($shopIds) {

                return $query->whereIn('shop_id', $shopIds);

            })->where('month4','=',4)

            ->where('cj_time','>',$jzxzTime)

            ->where('shop_id','not in',$shopIdmz)

            ->count();

        //流失客户

        $y =Db::name('customer')

            ->where($person_where)

            ->when($shopIds ?? [], function ($query) use ($shopIds) {

                return $query->whereIn('shop_id', $shopIds);

            })->where('month1','=',1)

            ->where('cj_time','>',$jzxzTime)

            ->where('shop_id','not in',$shopIdmz)

            ->count();

        $tenure =$tenure-$lc;

        $sysFinishEnterStore =$this->getCustomerCollect('count',$shopIds,$startTime,$endTime,$isShop,$shop_person_id);

        $sysFinishEnterStoreEcharts =$this->getCustomerCollect('echarts',$shopIds,$startTime,$endTime,$isShop,$shop_person_id);

        if($pc == 1){

            return $sysFinishEnterStoreEcharts;
            exit;

        }

        $customerBirthNumber =$this->getCustomerCollect('birth',$shopIds,$startTime,$endTime,$isShop,$shop_person_id);

        $customerNumber =$this->getCustomerCollect('network',$shopIds,$startTime,$endTime,$isShop,$shop_person_id);

        $customerTenureNumber =$this->getCustomerCollect('tenure',$shopIds,$startTime,$endTime,$isShop,$shop_person_id);

        $customerVisitedNumber =$this->getCustomerCollect('visited',$shopIds,$startTime,$endTime,$isShop,$shop_person_id);


        // if($sysFinishEnterStore ==0){

        //     $sysFinishEnterStore=1;

        // }
        // if($customerNumber == 0){
        //     $customerNumber =1;
        // }

        //

        // $source_ids =[43,44,118,50,51,52,54,55,56,53,584,609];
        $source_ids =[43,54,118,609,50,51,52,55,584];

        $cus =$this->whereIn('source_id',$source_ids)

                    ->when($shopIds ?? [], function ($query) use ($shopIds) {

                       return $query->whereIn('shop_id', $shopIds);

                    })

                    ->where(function($query) use ($startTime,$endTime){

                        $query->where([['first_time', 'between', "$startTime,$endTime"]])

                        ->whereOr([['second_time', 'between', "$startTime,$endTime"]]);

                    })

                    ->count();

        $sysOnlineStoreEntryRate= $customerNumber == '0' ? '0%' : (round($cus/$customerNumber,2)*100).'%';

        // echo "进店人数".$cus;
        // echo "<br/>";

        // echo "档案人数".$customerNumber;
        // echo "<br/>";
        // exit;

        return compact('sysFinishEnterStore','customerBirthNumber','customerNumber','sysOnlineStoreEntryRate','customerTenureNumber','customerVisitedNumber','tenure','y','lc','sysFinishEnterStoreEcharts');

    }

    /**

     * 根据场景值获取订单数据集合

     * @param $scene

     * @param $shopIds

     * @param $startTime

     * @param $endTime

     * @author ran

     * @date 2021-11-18 11:27

     * mailbox 466180170@qq.com

     */

    public function getCustomerCollect($scene,$shopIds, $startTime,$endTime,$isShop,$shop_person_id=0)
    {

        $sysModel = new SysModel();
        $positon_id = $sysModel->getPersonPositionId($shop_person_id);


        $is_shop_where = [];
        $mzShopID = [73782775377728,73782775392774,73782775206631,73782775131388,73782775085647,73782774991693,73782774881835];
        if($isShop){
            // echo "管理员查看";
            if($isShop=='1'){
                array_push($is_shop_where,['shop_id','not in',$mzShopID]);
            }else{
                array_push($is_shop_where,['shop_id','in',$mzShopID]);
            }
        }else{
            // echo "门店查看";
        }
        // exit;

        $connect =$this->where('effective_entry', 1)

            ->where($is_shop_where)


            ->when($shopIds ?? [], function ($query) use ($shopIds) {

                return $query->whereIn('shop_id', $shopIds);

            })->where(function($query) use ($shop_person_id,$positon_id){
                if(!empty($positon_id)){
                    $query->where('nurse_id',$shop_person_id);
                }
            });

        switch ($scene) {

            case 'count':

                return  $connect->where(function($query) use ($startTime,$endTime){

                    $query->where([['first_time|second_time', 'between', "$startTime,$endTime"]]);

                })->count();

                break;

            case 'echarts': // 疤痕 - 痘痘 - 项目

                $customerIds = $connect->where(function($query) use ($startTime,$endTime){

                    $query->where([['first_time|second_time', 'between', "$startTime,$endTime"]]);

                })->column('id');

                $bh = [1,2,3,4,5,6,7,8,655,656];

                $dd = [10];

                $xm = [9,11,12];

                $bhNum = Db::name('customer_scar')->where([['customer_id','in',$customerIds],['customer_setting_id_type','in',$bh]])->count();

                $ddNum = Db::name('customer_scar')->where([['customer_id','in',$customerIds],['customer_setting_id_type','in',$dd]])->count();

                $xmNum = Db::name('customer_scar')->where([['customer_id','in',$customerIds],['customer_setting_id_type','in',$xm]])->count();

                // echo "<pre>";
                // print_r($xmNum);
                // echo "</pre>";
                // exit;

                $data = [];

                $one['name'] = '疤痕进店';
                $one['value'] = $bhNum ?? 0;

                array_push($data,$one);

                $two['name'] = '痘痘进店';
                $two['value'] = $ddNum ?? 0;

                array_push($data,$two);

                $three['name'] = '项目进店';
                $three['value'] = $xmNum ?? 0;

                array_push($data,$three);

                return $data;

                break;

            case 'birth':

                $time =strtotime("+7 day");

                return $connect

                    ->whereTime('birthday', '>=', $time)

                    ->where('membership_leve','>',0)->count();

                break;

            case 'network':

                // $source_id =[43,44,118,50,51,52,54,55,56,53,584,609];
                $source_id =[43,54,118,609,50,51,52,55,584];


                return $this->whereIn('source_id',$source_id)

                    ->when($shopIds ?? [], function ($query) use ($shopIds) {

                       return $query->whereIn('shop_id', $shopIds);

                    })

                    ->where(function($query) use ($startTime,$endTime){

                        $query->where([['create_time', 'between', "$startTime,$endTime"]])

                              ->where([['shop_id', 'not in', "74076159037951"]]);

                            //->whereOr([['second_time', 'between', "$startTime,$endTime"]]);

                    })

                    ->count();

                break;

            case 'tenure':

                $source_id =[49];

                return $connect->whereIn('source_id',$source_id)

                     ->where(function($query) use ($startTime,$endTime){

                        $query->where([['first_time', 'between', "$startTime,$endTime"]])

                            ->whereOr([['second_time', 'between', "$startTime,$endTime"]]);

                    })->count();

                break;

            case 'visited':

                return $connect->where(function($query) use ($startTime,$endTime){

                    $query->where([['first_time', 'between', "$startTime,$endTime"]])

                        ->whereOr([['second_time', 'between', "$startTime,$endTime"]]);

                    })

                    ->where('last_kftime','<>',0)

                    ->where('cj_money','>',0)->count();

                break;

            default:

                break;

        }

    }



}
